import copy
import os.path
from Debug import debug, error
from EasyGuider import obj2ini
import Globals
import threading

_lock = threading.Lock()

class Preference():
    preflist = []
    pages_order = {}

    def __init__(self):
        self.preflist.sort()
        
    def get_defulat_inifile(self):
        return os.path.join(Globals.workpath, 'pref.ini')
    
    def clone(self):
        return copy.copy(self)

    def save(self, filename=''):
        _lock.acquire()
        try:
            if not filename:
                filename = self.get_defulat_inifile()
            try:
                obj2ini.dump(self, filename, encoding='utf-8')
            except:
                try:
                    obj2ini.dump(self, filename)
                except:
                    error.traceback()
        finally:
            _lock.release()

    def load(self, filename=''):
        if not filename:
            filename = self.get_defulat_inifile()
        if os.path.exists(filename):
            try:
                obj2ini.load(filename, obj=self, encoding='utf-8')
            except:
                try:
                    obj2ini.load(filename, obj=self)
                except:
                    error.traceback()

    def printValues(self):
        debug.info("[preference] member variable...")
        for k, v in self.__dict__.items():
            debug.info('\t', k,'=', v)
        debug.info('[preference] preference dialog member...')
        for v in self.preflist:
            debug.info('\t',v)
